from tornado.web import RequestHandler


class CookieHandler(RequestHandler):
    def get(self):
        #验证参数中是否存在name

        if self.request.arguments.get('name'):
            name = self.get_query_argument('name')
            # 从cookie中获取name的对象或值
            value = self.get_cookie(name)
            self.write(value)
        else:
            cookies:dict = self.request.cookies
            html = '<ul>%s</ul>'
            lis = []
            for k in cookies:
                lis.append("<li>%s:%s</li>"%(k,self.get_cookie(k)))
            html= '显示所有cookie'+html%''.join(lis)
            html +='''
                <form method='post'>
                    <input name='name' placeholder='请输入cookie的key'>
                    <button>提交</button>
                </form>
            '''
            self.write(html)
    def post(self):
        name = self.get_argument('name')
        if self.request.cookies.get(name,None):
            self.clear_cookie(name)
            self.write('<h3 style ="color:green;">删除%s成功</h3>'%name)
        else:
            self.write('<h3 style ="color:red;">删除%s失败,不存在!</h3>'%name)

        self.redirect('/cookie')